package com.pansoft.openplanet.util;

import com.pansoft.openplanet.blockchain.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TCStringUtil {

    public static boolean inputIsNumber(String text) {
        String regEx = "[^0-9.]";  //只能输入字母或小数点
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(text);
        String str = m.replaceAll("").trim();    //删掉不是字母或数字的字符
        return text.equals(str);

    }

    /**
     * 字符串转为*
     */
    public static String string2HideType(String s) {
        String hide = "";
        if (s == null) {
            return "*";
        }
        for (int i = 0; i < s.length(); i++) {
            hide += "*";
        }
        return hide;
    }

    /**
     * 字符串前四位和后四位不隐藏，每四个加一空格, 字符串为16位
     */
    public static String formatCardNoHide(String s) {
        if (StringUtils.isEmpty(s) || s.length() < 12) {
            return "";
        }
        String header = s.substring(0, 4) + " ";
        String middle = "**** **** ";
        String footer = s.substring(12);
        return header + middle + footer;
    }

    public static String formatCardNoShow(String s) {
        if (StringUtils.isEmpty(s) || s.length() < 12) {
            return "";
        }
        String header = s.substring(0, 4) + " ";
        String middle1 = s.substring(4, 8) + " ";
        String middle2 = s.substring(8, 12) + " ";
        String footer = s.substring(12);
        return header + middle1 + middle2 + footer;
    }

    public static void main(String[] args) {
        System.out.println(formatCardNoShow("TP02008600000001"));
    }
}

